﻿/*********************弘石项目三组************************
*  功能描述：运营管理控制器层
*  作者：周泽旭
*  时间：2021-12-10
*  修改人：周泽旭
*  修改时间：2021-12-10
*********************弘石项目三组************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Hong.Business.Business_OperationManagement;
using Hong.Model.SysDomain.Model_OperationManagement;

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using MiniExcelLibs;


namespace WeHongshibApI.Controllers.Controllers_OperationManagement
{
    [ApiExplorerSettings(GroupName = "Operation")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OperationController : ControllerBase
    {
        #region 构造器注入
        private readonly OperationBusiness operation;
        private readonly IHostingEnvironment hostingEnvironment;


        public OperationController(OperationBusiness _operation, IHostingEnvironment _IHostingEnvironment)
        {
            operation = _operation;
            hostingEnvironment = _IHostingEnvironment;
        }
        #endregion

        #region 运营管理

        #region 项目立项添加     
        [HttpPost]
        public IActionResult AddProject(Project p)
        {
            return Ok(operation.Add(p));
        }
        #endregion

        #region 项目列表显示
        [HttpGet]
        public IActionResult ShowProject(int pageindex , int pagesize , string ProjectName = "", string ProjectConId = "")
        {
            var list = operation.ShowProject(pageindex, pagesize, ProjectName, ProjectConId);
            return Ok(list);
        }
        #endregion

        #region 项目关联合同
        [HttpGet]
        public IActionResult ShowConPro(int pageindex, int pagesize, string ProjectName = "",string ProjectConId = "")
        {
            var list = operation.GetConPro(pageindex, pagesize, ProjectName, ProjectConId);
            return Ok(list);
        }
        #endregion

        #region 获取甲方联系人
        [HttpGet]
        public IActionResult ShowReponsble()
        {
            var list = operation.ShowReponsble();
            return Ok(list);
        }
        #endregion

        #region 获取合同信息
        [HttpGet]
        public IActionResult SelCon(string ContractName = "")
        {
            var list = operation.SelCon(ContractName);
            return Ok(list);
        }
        #endregion

        #region 获取部门信息
        [HttpGet]
        public IActionResult GetDept()
        {
            var list = operation.GetDept();
            return Ok(list);
        }
        #endregion

        #region 项目信息反填
        [HttpGet]
        public IActionResult FindPro(string ProjectId)
        {
            var list = operation.FindPro(ProjectId);
            return Ok(list);
        }
        #endregion

        #region 项目信息修改
        [HttpPut]
        public IActionResult UptPro(Project p)
        {
            return Ok(operation.UptPro(p));
        }
        #endregion

        #region 项目信息导出Excel
        [HttpGet]
        public ActionResult GetContractExcel()
        {

            var list = operation.GetPro();
            var values = new List<Dictionary<string, string>>();
            foreach(var item in list)
            {
                values.Add(new Dictionary<string, string>
                {
                    { "序号",$"{item.ProjectId}" },{ "甲方负责人",$"{item. DName}"},{ "项目名称",$"{item. ProjectName}"},{ "建筑类别",$"{item. ProjectBuildtype}"},{ "合同信息",$"{item. CName}"},{ "合同额（万元）",$"{item. ProjectPrice}"},{ "开始日期",$"{item. ProjectStartTime}"},{ "结束日期",$"{item. ProjectEndTime}"}
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "applicatio/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "项目信息.xlsx"
            };
        }
        #endregion

        #region 项目关联合同导出Excel
        [HttpGet]
        public ActionResult GetProExcel()
        {
            var list = operation.GetProExcel();
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(list);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "applicatio/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "项目关联合同.xlsx"
            };
        }
        #endregion

        #region 项目关联合同添加
        [HttpPost]
        public IActionResult AddConpro(int pid, int cid)
        {
            return Ok(operation.AddCon(pid, cid));
        }
        #endregion

        #region 项目进度统计
        [HttpGet]
        public IActionResult GetProgress(int pageindex, int pagesize, string ProjectName = "",string ProjectDeptId="")
        {
            return Ok(operation.GetProgress(pageindex, pagesize, ProjectName, ProjectDeptId));
        }
        #endregion

        #region 项目进度统计导出
        [HttpGet]
        public IActionResult GetGrassExcel()
        {
            var list = operation.GetGrassExcel();
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(list);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "applicatio/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "项目进度统计.xlsx"
            };
        }
        #endregion

        #region 项目统计
        [HttpGet]
        public IActionResult ShowCountProject(string DName)
        {
            var list = operation.ShowCountProject(DName);
            return Ok(list);
        }
        #endregion

        #region 上传图片
        [HttpPost]
        public IActionResult UpLoad()
        {
            //var postFile = HttpContext.Request.Form.Files[0];
            //string newFileName = Guid.NewGuid() + "-" + postFile.FileName;
            //string savePath = Directory.GetCurrentDirectory() + "/Images/" + newFileName;
            //using (FileStream fs=new FileStream(savePath, FileMode.Create))
            //{
            //    postFile.CopyTo(fs);
            //    fs.Flush();
            //}
            //return Content("https://localhost:44393/Images/"+newFileName);


            //http 协议 Request Response
            //获取文件
            var hpf = Request.Form.Files[0];
            //新名称
            var newFileName = Guid.NewGuid().ToString();
            //获取扩展名
            var ext = Path.GetExtension(hpf.FileName);
            //将文件名和扩展名拼接
            var newName = newFileName + ext;
            //找到路径  将虚拟路径转成物理路径
            var path = Directory.GetCurrentDirectory() + "/Images";
            //合并路径和文件名
            var newPath = Path.Combine(path, newName);

            using (var stream = System.IO.File.Create(newPath))
            {
                hpf.CopyTo(stream);
            }
            return Content("https://localhost:44393/Images/" + newName);
        }
        #endregion



        #endregion
    }

}
